﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using AutoLoL.Entities;
using AutoLoL.Logic;

namespace AutoLoL.Controls
{
    /// <summary>
    /// Interaction logic for MasteryTreeControl.xaml
    /// </summary>
    public partial class MasteryTreeControl : UserControl
    {
        public static DependencyProperty MasteryTreeProperty = DependencyProperty.Register("MasteryTree", typeof(MasteryTree), typeof(MasteryTreeControl));

        public MasteryTree MasteryTree
        {
            get { return (MasteryTree)GetValue(MasteryTreeProperty); }
            set
            {
                SetValue(MasteryTreeProperty, value);
                InitUI();
            }
        }

        public MasteryTreeControl(MasteryTree masteryTree)
        {
            InitializeComponent();
            MasteryTree = masteryTree;
        }

        private void InitUI()
        {
            // Clear
            MasteriesGrid.Children.Clear();
            MasteriesGrid.RowDefinitions.Clear();
            MasteriesGrid.ColumnDefinitions.Clear();

            // Create row and column definitions
            int rows = MasteryTree.MasteryValues.Select(m => m.Row).Max();
            for (int i = 0; i <= rows; i++)
            {
                MasteriesGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(52) });
            }

            int cols = MasteryTree.MasteryValues.Select(m => m.Column).Max();
            for (int i = 0; i <= cols; i++)
            {
                MasteriesGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(43) });
            }

            // Add masteries
            foreach (MasteryValue mastery in MasteryTree.MasteryValues)
            {
                MasteryControl masteryControl = new MasteryControl(mastery);
                masteryControl.SetValue(Grid.ColumnProperty, mastery.Column);
                masteryControl.SetValue(Grid.RowProperty, mastery.Row);
                MasteriesGrid.Children.Add(masteryControl);
            }
        }
    }
}
